import java.util.Arrays;

public class Solution662 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int max=1;
    int[] min=new int[3005];

    public int widthOfBinaryTree(TreeNode root) {
        Arrays.fill(min,-1);
        dfs(root,1,0);
        return max;
    }

    private void dfs(TreeNode root, int k,int deep) {
        if (root==null){
            return;
        }
        if (min[deep]==-1){
            min[deep]=k;
        }else {
            max=Math.max(max,k-min[deep]+1);
        }
        dfs(root.left,k*2,deep+1);
        dfs(root.right,k*2+1,deep+1);
    }

    public static void main(String[] args) {
        System.out.println(new Solution662().widthOfBinaryTree(new TreeNode(1,new TreeNode(3,new TreeNode(5),new TreeNode(3)),new TreeNode(2,null,new TreeNode(9)))));
    }
}
